package main;

import javax.swing.JPanel;
import java.awt.GridBagLayout;
import javax.swing.JTable;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import org.eclipse.wb.swing.FocusTraversalOnArray;

import sun.beans.editors.BooleanEditor;

import com.aptech.mwg.etown.pojo.Account;
import com.aptech.mwg.etown.pojo.Floor;
import com.aptech.mwg.etown.pojo.exception.SelectDBException;
import com.aptech.mwg.etown.server.business.AccountManager;
import com.aptech.mwg.etown.server.business.FloorManager;

import java.awt.Component;
import javax.swing.Box;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ScrollPaneConstants;
import javax.swing.JInternalFrame;
import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class FloorManage extends JPanel {
        private JTable table;
        
        private JTextField txtuser;
        private JTextField txtEmail;
        private JPanel panelsearch ;
        
        FloorManager floorManager = new FloorManager();
        /**
         * Create the panel.
         */
        public FloorManage() {
                setBorder(null);
                GridBagLayout gridBagLayout = new GridBagLayout();
                gridBagLayout.columnWidths = new int[]{0, 0, 0, 0};
                gridBagLayout.rowHeights = new int[]{147, 0, 40, 39, 0};
                gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
                gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
                setLayout(gridBagLayout);
                
                Component horizontalGlue = Box.createHorizontalGlue();
                GridBagConstraints gbc_horizontalGlue = new GridBagConstraints();
                gbc_horizontalGlue.insets = new Insets(0, 0, 5, 5);
                gbc_horizontalGlue.gridx = 0;
                gbc_horizontalGlue.gridy = 0;
                add(horizontalGlue, gbc_horizontalGlue);
                
                final JPanel panel = new JPanel();
                GridBagConstraints gbc_panel = new GridBagConstraints();
                gbc_panel.insets = new Insets(0, 0, 5, 5);
                gbc_panel.fill = GridBagConstraints.BOTH;
                gbc_panel.gridx = 1;
                gbc_panel.gridy = 0;
                add(panel, gbc_panel);
                
                
                table = new JTable();
                table.setModel(new DefaultTableModel(
                        new Object[][] {
                                
                        },
                        new String[] {
                                "", "Floor No.", "Description"
                        }
                ));
                table.getColumnModel().getColumn(0).setPreferredWidth(49);
                table.getColumnModel().getColumn(1).setPreferredWidth(114);
                table.getColumnModel().getColumn(2).setPreferredWidth(114);
                table.getColumnModel().getColumn(3).setPreferredWidth(114);
                table.getColumnModel().getColumn(4).setPreferredWidth(114);
                
                panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
                
                JScrollPane scrollPane = new JScrollPane();
                scrollPane.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mousePressed(MouseEvent e) {
                        
                        
                        
                    }
                });
                scrollPane.setViewportView(table);
                scrollPane.setPreferredSize(new Dimension(500, 150));
                panel.add(scrollPane);
                
                Component horizontalGlue_1 = Box.createHorizontalGlue();
                GridBagConstraints gbc_horizontalGlue_1 = new GridBagConstraints();
                gbc_horizontalGlue_1.insets = new Insets(0, 0, 5, 0);
                gbc_horizontalGlue_1.gridx = 2;
                gbc_horizontalGlue_1.gridy = 0;
                add(horizontalGlue_1, gbc_horizontalGlue_1);
                
                Component verticalGlue_1 = Box.createVerticalGlue();
                GridBagConstraints gbc_verticalGlue_1 = new GridBagConstraints();
                gbc_verticalGlue_1.insets = new Insets(0, 0, 5, 5);
                gbc_verticalGlue_1.gridx = 1;
                gbc_verticalGlue_1.gridy = 1;
                add(verticalGlue_1, gbc_verticalGlue_1);
                
                final JPanel panel_1 = new JPanel();
                GridBagConstraints gbc_panel_1 = new GridBagConstraints();
                gbc_panel_1.insets = new Insets(0, 0, 5, 5);
                gbc_panel_1.fill = GridBagConstraints.BOTH;
                gbc_panel_1.gridx = 1;
                gbc_panel_1.gridy = 2;
                add(panel_1, gbc_panel_1);
                panel_1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
                
                JButton btnSearch = new JButton("Search");
                btnSearch.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                                panelsearch.setVisible(true);
                        }
                });
                panel_1.add(btnSearch);
                
                JButton btnRent = new JButton("Rent");
                btnRent.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                                
                        }
                });
                panel_1.add(btnRent);
                
                JButton btncancel = new JButton("Cancel");
                btncancel.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                    }
                });
                btncancel.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                                panel.setVisible(false);
                                panel_1.setVisible(false);
                
                        }
                });
                panel_1.add(btncancel);
                
                panelsearch = new JPanel();
                GridBagConstraints gbc_panelsearch = new GridBagConstraints();
                gbc_panelsearch.insets = new Insets(0, 0, 0, 5);
                gbc_panelsearch.fill = GridBagConstraints.BOTH;
                gbc_panelsearch.gridx = 1;
                gbc_panelsearch.gridy = 3;
                add(panelsearch, gbc_panelsearch);
                GridBagLayout gbl_panelsearch = new GridBagLayout();
                gbl_panelsearch.columnWidths = new int[]{0, 39, 0, 0};
                gbl_panelsearch.rowHeights = new int[]{0, 0, 0, 0};
                gbl_panelsearch.columnWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
                gbl_panelsearch.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
                panelsearch.setLayout(gbl_panelsearch);
                
                JPanel panel_3 = new JPanel();
                GridBagConstraints gbc_panel_3 = new GridBagConstraints();
                gbc_panel_3.insets = new Insets(0, 0, 5, 5);
                gbc_panel_3.fill = GridBagConstraints.BOTH;
                gbc_panel_3.gridx = 1;
                gbc_panel_3.gridy = 0;
                panelsearch.add(panel_3, gbc_panel_3);
                panel_3.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
                
                JLabel lblUsername = new JLabel("UserName: ");
                panel_3.add(lblUsername);
                
                txtuser = new JTextField();
                panel_3.add(txtuser);
                txtuser.setColumns(10);
                
                Component horizontalStrut_1 = Box.createHorizontalStrut(20);
                panel_3.add(horizontalStrut_1);
                
                JLabel lblCompany = new JLabel("Email");
                panel_3.add(lblCompany);
                
                txtEmail = new JTextField();
                panel_3.add(txtEmail);
                txtEmail.setColumns(10);
                
                Component verticalGlue_3 = Box.createVerticalGlue();
                GridBagConstraints gbc_verticalGlue_3 = new GridBagConstraints();
                gbc_verticalGlue_3.insets = new Insets(0, 0, 5, 5);
                gbc_verticalGlue_3.gridx = 1;
                gbc_verticalGlue_3.gridy = 1;
                panelsearch.add(verticalGlue_3, gbc_verticalGlue_3);
                
                JPanel panel_4 = new JPanel();
                GridBagConstraints gbc_panel_4 = new GridBagConstraints();
                gbc_panel_4.insets = new Insets(0, 0, 0, 5);
                gbc_panel_4.fill = GridBagConstraints.BOTH;
                gbc_panel_4.gridx = 1;
                gbc_panel_4.gridy = 2;
                panelsearch.add(panel_4, gbc_panel_4);
                
                panelsearch.setVisible(false);
                JButton btnOk = new JButton("Ok");
                btnOk.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        FloorManager floorManager = new FloorManager();
                        DefaultTableModel model = (DefaultTableModel)table.getModel();
            while(model.getRowCount() > 0)
            {
            model.removeRow(0);
            }

            List<Floor> list = floorManager.Search_condition(txtuser.getText(),txtEmail.getText());
            for(Floor floor: list){
            String floorName = floor.getFloorName();
            String description = floor.getDescription();
            Object[] newRow = new Object[] {new Boolean(false), floorName, description};
            model.addRow(newRow);
            System.out.println("load success" );
            }
                    }
                });
                btnOk.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                                
                        }
                });
                panel_4.add(btnOk);
                
                Component horizontalStrut_2 = Box.createHorizontalStrut(20);
                panel_4.add(horizontalStrut_2);
                
                JButton btnReset = new JButton("Reset");
                btnReset.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            txtEmail.getText();
                            txtuser.getText();
                        }
                });
                btnReset.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                        }
                });
                panel_4.add(btnReset);
                
                Component horizontalStrut_4 = Box.createHorizontalStrut(20);
                panel_4.add(horizontalStrut_4);
                
                JButton btnCancel = new JButton("Cancel");
                btnCancel.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                                panelsearch.setVisible(false);
                        }
                });
                panel_4.add(btnCancel);
                
                
        }
        
        public void loadtable (){
                 
                DefaultTableModel model = (DefaultTableModel)table.getModel();
                model.setRowCount(0);
                List<Floor> listFloor = floorManager.list();
                for (Floor floor : listFloor) {
                        String floorName = floor.getFloorName();
                        String description = floor.getDescription();
                        Object[] newRow = new Object[] {new Boolean(false), floorName, description};
                        model.addRow(newRow);
                        System.out.println("load success" );
                }
                table.repaint();
         }
                
}
